package com.jiayou.activity.adapter

import android.content.Context
import android.graphics.BitmapFactory
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import cn.hutool.http.HttpRequest
import cn.hutool.json.JSONUtil
import com.jiayou.R
import com.jiayou.activity.MainActivity
import com.jiayou.activity.MomentsActivity
import com.qmuiteam.qmui.widget.dialog.QMUIDialog.MessageDialogBuilder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlin.concurrent.thread


class MomentAdapter(var context: Context, var list: ArrayList<Moment>) : RecyclerView.Adapter<MomentAdapter.MomentViewHolder>() {

    class MomentViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var constraintLayout: ConstraintLayout? = null
        var head: ImageView? = null
        var petname: TextView? = null
        var description: TextView? = null

        init {
            constraintLayout = view as ConstraintLayout
            head = view.findViewById(R.id.imageView5)
            petname = view.findViewById(R.id.textView4)
            description = view.findViewById(R.id.description)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MomentViewHolder {
        return MomentViewHolder(
            LayoutInflater.from(context).inflate(
                R.layout.moments_item,
                parent,
                false
            )
        )
    }

    override fun getItemCount() = list.size

    override fun onBindViewHolder(holder: MomentViewHolder, position: Int) {
        val moment = list[position]
        holder.petname?.text = moment.petname
        holder.description?.text = moment.description
        thread {
            val bodyStream = HttpRequest.get("http://${(context as MomentsActivity).uri}/im/userinfo/getImgById").form("id", moment.username)
                .header("auth", "73913da7-508b-4646-b8ae-ef6458f41153")
                .execute()
                .bodyStream()
            runBlocking {
                withContext(Dispatchers.Main) {
                    holder.head?.setImageBitmap(BitmapFactory.decodeStream(bodyStream))
                    holder.constraintLayout?.setOnLongClickListener {
                        if (moment.username == MainActivity.ID) {
                            MessageDialogBuilder(context)
                                .setMessage("确认要删除该条朋友圈嘛?")
                                .setTitle("朋友圈删除")
                                .addAction(
                                    "取消"
                                ) { dialog, index -> dialog.dismiss() }
                                .addAction(
                                    "确认"
                                ) { dialog, index ->
                                    thread {
                                        val body = JSONUtil.parseObj(
                                            HttpRequest.post("http://${(context as MomentsActivity).uri}/im/moment/del").form("id", moment.id)
                                                .header("auth", "73913da7-508b-4646-b8ae-ef6458f41153")
                                                .execute()
                                                .body()
                                        )
                                        runBlocking {
                                            withContext(Dispatchers.Main) {
                                                Toast.makeText(context, body["data"].toString(), Toast.LENGTH_SHORT).show()
                                                if (body["code"].toString().toInt() == 200) {
                                                    list.removeAt(position)
                                                    notifyItemRemoved(1)
                                                    notifyDataSetChanged()
                                                }
                                            }
                                        }
                                    }
                                    dialog.dismiss()
                                }
                                .create().show()
                        }
                        true
                    }
                }
            }

        }
    }


    override fun getItemViewType(position: Int) = position
}